//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
    components: {//动态扩充组件或组件路径
        //表单header、content、footer对应位置扩充的组件
        gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
        gridBody: '',
        gridFooter: '',
        //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
        modelHeader: '',
        modelBody: '',
        modelFooter: ''
    },
    buttons: { view: [], box: [], detail: [] },//扩展的按钮
    methods: {//事件扩展
        getFormOption (field) {
            let option;
            this.editFormOptions.forEach(x => {
                x.forEach(item => {
                    if (item.field == field) {
                        option = item;
                    }
                })
            })
            return option;
        },
        getDetailOption (field) {
            let option;
            this.detail.columns.forEach(item => {
                if (item.field == field) {
                    option = item;
                }
            })
            return option;
        },
        onInit () {
            let type = this.getFormOption('type')
            let supplierCode = this.getFormOption('type')
            type.onChange = (e) => {
                this.getList()
            }
            let desc = this.getFormOption('desc')
            this.http.post('/api/Dictionary/GetVueDictionary', ['mall_test']).then(res => {
                desc.data = res[0].data.filter(i => {
                    if (i.remark == 'YingFang') {
                        return i
                    }
                })
            })
            // supplierCode.onChange = (e) => {
            //     this.getList()
            // }
        },
        getList () {
            if (this.editFormFields.campUnitId != "") {
                let parma = {
                    code: this.editFormFields.campUnitId,
                    type: this.editFormFields.type
                }
                this.http.get("/api/CampHouseType/GetHouseType", parma).then(res => {
                    let houseType = this.getDetailOption('houseType')
                    houseType.bind.data = res.data.map(i => {
                        return {
                            key: i.id,
                            value: i.name
                        }
                    })
                })
            } else {
                this.$Message.error("请先输入单位ID")
            }

        },
        rowClick ({ row, column, event }) {
            this.$refs.table.$refs.table.toggleRowSelection(row);
            return true;
        },
        addBefore (formData) {
            return this.veriFromTable(formData)
        },
        updateBefore (formData) {
            return this.veriFromTable(formData)
        },
        // 保存前编辑 
        veriFromTable (formData) {
            // console.log(formData);
            let detailData = formData.detailData
            if (detailData.length > 0) {
                for (let index = 0; index < detailData.length; index++) {
                    let element = detailData[index];
                    let timeContainer = element.timeContainer
                    if (!timeContainer) {
                        this.$Message.error("请选择起止时间！")
                        return false
                    }

                    if (timeContainer.length > 0 && timeContainer[0] != "") {
                        element.startTime = timeContainer[0]
                        element.endTime = timeContainer[1]
                    } else {
                        this.$Message.error("请选择起止时间！")
                        return false
                    }
                    element.campHouseType = formData.mainData.type
                    delete element.timeContainer
                    element.dayNum = element.dayNum == 1 ? 1 : 0
                }
                return true
            } else {
                return true
            }
        },
        modelOpenBefore (row) {  //点击编辑/新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
            if (this.currentAction == 'Add') {

            } else {
                this.editFormFields.supplierCode = row.supplierCode
                this.editFormFields.type = row.type
                this.editFormFields.campUnitId = row.campUnitId
                this.getList()
            }
        },
        searchDetailAfter (data) {
            data.forEach(item => {
                item.timeContainer = [item.startTime, item.endTime]
            })
            return true;
        },
        addRow () {
            let houseType = this.getDetailOption('houseType')
            if (houseType.bind.data.length != 0) {
                let obj = {
                    houseType: houseType.bind.data[0].key,
                    timeContainer: []
                };

                this.$refs.detail.addRow(obj);
            } else {
                this.$Message.error('请选择营房类型有房型的数据')
            }
        }
    }
};
export default extension;